package com.quzi.kettle.activitise.added;

import android.media.RingtoneManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.utils.TipvoiceUtil;

public class TipVoiceActivity extends BaseActivity {
	private ScrollView sv_tipvoice;
	private ListViewForScrollView lv_tipvoice;
	private TipVoiceAdapter adapter;
	// private int Checked = 0;
	// List<String> voicenamelist = new ArrayList<String>();
	private TextView tv_tipvoice_change_voicerecord;
	private RelativeLayout rela_tipvoice_record;
	private ImageView iv_tipvoice_record;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tipvoice);
		sv_tipvoice = (ScrollView) findViewById(R.id.sv_tipvoice);
		sv_tipvoice.smoothScrollBy(0, 0);
		tv_tipvoice_change_voicerecord = (TextView) findViewById(R.id.tv_tipvoice_change_voicerecord);
		tv_tipvoice_change_voicerecord.setOnClickListener(this);
		rela_tipvoice_record = (RelativeLayout) findViewById(R.id.rela_tipvoice_record);
		rela_tipvoice_record.setOnClickListener(this);
		iv_tipvoice_record = (ImageView) findViewById(R.id.iv_tipvoice_record);
		if (TipvoiceUtil.getVoiceNo(getApplicationContext()) == -1) {
			iv_tipvoice_record.setVisibility(View.VISIBLE);
		}
		initlv();

	}

	private void initlv() {
		lv_tipvoice = (ListViewForScrollView) findViewById(R.id.lv_tipvoice);

		adapter = new TipVoiceAdapter(getApplicationContext(),
				TipvoiceUtil.getVoiceNo(getApplicationContext()));
		lv_tipvoice.setAdapter(adapter);
		lv_tipvoice.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				adapter.setcheck(position);
				iv_tipvoice_record.setVisibility(View.INVISIBLE);
				TipvoiceUtil.saveVoiceNo(getApplicationContext(), position);
				TipvoiceUtil.saveVoiceName(getApplicationContext(),
						(String) adapter.getItem(position));
			
				try {

					RingtoneManager rm = new RingtoneManager(
							TipVoiceActivity.this);
					rm.setType(RingtoneManager.TYPE_NOTIFICATION);
					rm.getCursor();
					rm.getRingtone(position).play();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		VoiceRecordUtil.Destory();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_tipvoice_change_voicerecord:
			nextActivity(VoiceRecordActivity.class);
			break;
		case R.id.rela_tipvoice_record:
			adapter.setcheck(-1);
			iv_tipvoice_record.setVisibility(View.VISIBLE);
			TipvoiceUtil.saveVoiceNo(getApplicationContext(), -1);
			TipvoiceUtil.saveVoiceName(getApplicationContext(), getResources()
					.getString(R.string.voicerecord));
			VoiceRecordUtil.startPlay(getApplicationContext());
			break;
		default:
			break;
		}
	}

}
